package plugins.debugeTool
{
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.core.effects.view.ReflectionView;
	import org.papervision3d.core.proto.SceneObject3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.view.Viewport3D;
	
	import pv3d.cameras.RotateableFreeCamera3D;
	
	public class PicturePlayer3D extends Sprite
	{
		private var renderer:BasicRenderEngine;           //3D场景的渲染器
		private var container:SceneObject3D;              //3D场景模块的容器
		private var camera:RotateableFreeCamera3D;        //3D场景的镜头
		private var viewport:Viewport3D;                  //3D场景的视窗
		private var angleUnit	:Number=(Math.PI * 2) /8;
		private var radius:Number=369/Math.sin(angleUnit/2);
		private var planeContainer:DisplayObject3D;
		private var isDown:Boolean=false;
		private var box:DAE;
		private var circle1:DAE;
		private var planes:Array=new Array();
		private var loader:QueueLoader;
		private var circle3:DAE;
		private var reflectionView:ReflectionView;
		public function PicturePlayer3D()
		{
			create3D();
		}
		private function create3D():void
		{
			reflectionView=new ReflectionView(900,480,false,false,CameraType.FREE);
			this.addChild(reflectionView);
//			renderer = new BasicRenderEngine();
//			container = new SceneObject3D();
//			viewport = new Viewport3D(900, 480, false, false);
//			viewport.interactive=true;
//			this.addChild(viewport);
//			camera = new RotateableFreeCamera3D();
			reflectionView.camera.focus=25;
			reflectionView.camera.z=-6000;
			reflectionView.camera.y=400;
			planeContainer=new DisplayObject3D();
			reflectionView.scene.addChild(planeContainer);
			reflectionView.surfaceHeight=-800;
			reflectionView.viewportReflection.alpha=0.3;
			reflectionView.viewportReflection.filters=[new BlurFilter(4,4,1)];
			planeContainer.rotationZ=-25;
			planeContainer.rotationX=25;
			createPlanes();
			var list:MaterialsList=new MaterialsList();
			list.addMaterial(new ColorMaterial(0xffffff,0.7),"all");
			var list1:MaterialsList=new MaterialsList();
			list1.addMaterial(new BitmapFileMaterial("image/picturePlayer3d/111.png"),"all");
			box=new DAE(true,"box");
			box.y=-500;
			circle1=new DAE(false,"circle1");
			circle1.y=-500;
			circle3=new DAE(false);
			circle3.y=-500;
			circle3.load("image/picturePlayer3d/circle3.jpg",list);
			circle3.rotationY=210;
			box.load("image/picturePlayer3d/circle.jpg",list);
			var list2:MaterialsList=new MaterialsList();
			list2.addMaterial(new BitmapFileMaterial("image/picturePlayer3d/nihao.png"),"all");
			circle1.load("image/picturePlayer3d/circle1.jpg",list2);
			reflectionView.scene.addChild(circle1);
//			reflectionView.scene.addChild(box);
//			reflectionView.scene.addChild(circle3);
			this.addEventListener(Event.ENTER_FRAME,on_enter);
			this.addEventListener(MouseEvent.MOUSE_DOWN,on_down);
			this.addEventListener(MouseEvent.MOUSE_UP,on_up);
		}
		private function on_down(e:MouseEvent):void
		{
			isDown=true;
		}
		private function on_up(e:MouseEvent):void
		{
			isDown=false;
		}
		private function createPlanes():void
		{
			var i:int=0;
			loader=new QueueLoader();
			for(i=0;i<8;i++)
			{
				createPlane(i);
				loader.addItem("image/picturePlayer3d/"+i.toString()+".jpg");
			}
			loader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,on_item_complete);
			loader.execute();
		}
		private var index:int=-1;
		private function on_item_complete(e:QueueLoaderEvent):void
		{
			index++;
			var plane:Plane=planes[index];
			plane.material=new BitmapMaterial(Bitmap(e.content).bitmapData);
			plane.material.doubleSided=true;
			plane.material.smooth=true;
		}
		private function createPlane(id:int):void
		{
			var _radian	:Number = id * angleUnit;
			var color:uint=0x00ffff;
			if(id%2==0) color=0x0000ff;
			var plane:Plane=new Plane(new ColorMaterial(color, 1), 800, 600, 4, 4);
			plane.material.doubleSided=true;
			plane.x =Math.cos(_radian) * radius;
			plane.z = Math.sin(_radian) * radius;
			plane.rotationY = 270 - (_radian * 180 / Math.PI) ;
			planeContainer.addChild(plane);
			planes.push(plane);
		}
		private var offset:int=-1;
		private function on_enter(e:Event):void
		{
//			if(!isDown)planeContainer.rotationY-=8;
			planeContainer.localRotationY-=3;
			box.rotationY+=8;
			circle1.rotationY-=8;
			circle3.rotationY+=2*offset;
			if(circle3.rotationY<200 || circle3.rotationY>260) offset*=-1;
			reflectionView.singleRender();
		}
	}
}